package com.pmcc.core.web;

import com.pmcc.core.config.utils.AntPageRequest;
import com.pmcc.core.entity.UUIDEntity;
import com.pmcc.core.entity.enums.ModuleType;
import com.pmcc.core.entity.security.Authority;
import com.pmcc.core.entity.security.Role;
import com.pmcc.core.repository.AuthorityRepository;
import com.pmcc.core.repository.RoleRepository;
import com.pmcc.core.service.AuthorityService;
import com.pmcc.utils.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.repository.query.QueryByExampleExecutor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by zhou on 2017/8/14.
 */
@RestController
@RequestMapping("api/v1/authority")
public class AuthorityController {
   /* @Autowired
    private AuthorityRepository authorityRepository;

    @Autowired
    private QueryByExampleExecutor<Authority> queryByExampleExecutor;

    @Autowired
    private AuthorityService authorityService;

    @Autowired
    RoleRepository roleRepository;*/

    /**
     * 查询
     * @param pageRequest
     * @return
     */
   /* @GetMapping("")
    public Page<Authority> findAll(AntPageRequest pageRequest,Authority authority){
        Page<Authority> page;
        if(authority.getParentId()==null){
            page=authorityRepository.findAll(pageRequest);
        }else{
            page=authorityRepository.findByparentIdOrId(authority.getParentId(),authority.getParentId(),pageRequest);
        }
        return page;
    }
    *//**
     * 新增菜单
     * @param authority
     * @return
     *//*
    @PostMapping()
    public ResultBean create(@RequestBody Authority authority){
        ResultBean resultBean=new ResultBean(ResultBean.SUCCESS);
        if(authority.getParentId()==null||authority.getParentId().equals("")){
            authority.setParentId("-1");
        }else{
            Authority parentAuthority=authorityRepository.findOne(authority.getParentId());
            //如果徐那种增加的父树根类型是叶子，则改为根
            if(parentAuthority!=null && parentAuthority.getLeaf()==true){
                parentAuthority.setLeaf(false);
                authorityRepository.save(parentAuthority);
            }
        }
        authority.setCreateTime(new Date());
        authority.setModuleType(ModuleType.MENU);
        authority.setIcon("camera-o");
        authority.setLeaf(true);
        authority.setSortNo(1);
        authorityRepository.save(authority);
        return resultBean;
    }

  *//*  *//**//**
     * 修改
     * @param authority
     * @return
     *//**//*
    @PatchMapping()
    public Authority update(@RequestBody Authority authority){
        Authority old=authorityRepository.findOne(authority.getId());
        old.setName(authority.getName());
        old.setCode(authority.getCode());
        authorityRepository.save(old);
        return old;
    }*//*

   *//* *//**//**
     * 删除本菜单下的所有菜单 包括本菜单
     * @param authority
     *//**//*
    @DeleteMapping()
    public void delete(@RequestBody Authority authority){
        Authority old=authorityRepository.findOne(authority.getId());
        List<Authority> list=authorityRepository.findByparentId(old.getId());//查询下级所有菜单
        for (Authority a:list) {
            authorityRepository.delete(a);
        }
        if(old.getParentId()!=null){
            List<Authority> authorityList=authorityRepository.findByparentId(old.getParentId());//查询同级所有菜单
            if(authorityList.size()==1){//如果长度为1，则代表上级菜单下级只有该菜单
                Authority upOld=authorityRepository.findOne(old.getParentId());
                upOld.setLeaf(true);
                authorityRepository.save(upOld);
            }
        }
        authorityRepository.delete(authority);

    }*//*

    *//**
     * 删除
     * @param map
     * @return
     *//*
    @DeleteMapping()
    @ResponseBody
    public ResultBean delete(@RequestBody Map<String, String> map){
        ResultBean resultBean=new ResultBean(ResultBean.SUCCESS);
        if(map.containsKey("id")){
            if(map.get("id")!=null){
                String  id=map.get("id").toString();
                    try {
                        List<Authority> authorityList = authorityRepository.findByParentId(id);
                        authorityList.add(authorityRepository.findOne(id));

                        Authority authority = authorityRepository.findOne(id);
                        List<Role> roleList = null;
                        if (roleList != null && roleList.size() > 0) {
                            for (Role role : roleList) {
                                Set<Authority> set = null;
                                if (set != null && set.size() > 0) {
                                    set.removeAll(authorityList);
                                    roleRepository.save(role);
                                    continue;
                                }

                            }
                        }
                        List<Authority> list = authorityRepository.findByParentId(id);
                        if (list != null && list.size() > 0) {
                            for (int i = 0; i < list.size(); i++) {
                                authorityRepository.delete(list.get(i).getId());
                            }
                        }
                    } catch (Exception e) {
                        resultBean.setResultCode(ResultBean.FAIL);
                    }
                    authorityRepository.delete(id);
                }
            }
        return resultBean;
    }

    *//**
     * 修改
     *
     * @param map
     * @return
     *//*
    @PutMapping()
    public ResultBean update(@RequestBody Map<String,String> map){
        return  authorityService.update(map);
    }*/
}
